package cn.itcast.service.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service(timeout = 300000)
public class ExtCproductServiceImpl implements ExtCproductService {

    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;
    //分页
    @Override
    public PageInfo<ExtCproduct> findByPage(ExtCproductExample extCproductExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ExtCproduct> list = extCproductDao.selectByExample(extCproductExample);

        return new PageInfo<>(list);
    }

    //查询所有
    @Override
    public List<ExtCproduct> findAll(ExtCproductExample extCproductExample) {
        return extCproductDao.selectByExample(extCproductExample);
    }

    //根据id查询
    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    //修改保存
    @Override
    public void save(ExtCproduct extCproduct) {
        //设置id
        extCproduct.setId(UUID.randomUUID().toString());
        //计算附件总金额
        Double amount = 0d;
        if(extCproduct.getCnumber() != null && extCproduct.getPrice() !=null){

            amount= extCproduct.getCnumber()* extCproduct.getPrice();
        }
        //设置附件金额
        extCproduct.setAmount(amount);

        //修改购销合同 通过购销合同id查询
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());

        //获取合同金额 计算添加后的金额
        Double totalAmount = contract.getTotalAmount();
        totalAmount += amount;
        contract.setTotalAmount(totalAmount);
        //获取附件数
        Integer extNum = contract.getExtNum();
        extNum += 1;
        contract.setExtNum(extNum);
        //修改更新合同
        contractDao.updateByPrimaryKeySelective(contract);

        extCproductDao.insertSelective(extCproduct);

    }

    //更新用户
    @Override
    public void update(ExtCproduct extCproduct) {
        //计算修改后的金额
        Double amount = 0d;
        if(extCproduct.getCnumber()!=null && extCproduct.getPrice() !=null){
            amount= extCproduct.getCnumber() * extCproduct.getPrice();
        }
        extCproduct.setAmount(amount);

        //计算修改前的附件总金额
        ExtCproduct excp = extCproductDao.selectByPrimaryKey(extCproduct.getId());
        Double oldamount = excp.getAmount();

        //修改购销合同  通过购销合同查询购销合同总金额
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        Double totalAmount = contract.getTotalAmount();

        //总金额=总金额+修改后 -修改前
        totalAmount = totalAmount +amount -oldamount;
        contract.setTotalAmount(totalAmount);

        contractDao.updateByPrimaryKeySelective(contract);

        //修改附件
        extCproductDao.updateByPrimaryKeySelective(extCproduct);
    }

    //根据id删除
    @Override
    public void delete(String id) {
        //计算删除的附件的金额  根据附件id查询
        ExtCproduct  exct = extCproductDao.selectByPrimaryKey(id);
        Double amount = exct.getAmount();

        //修改合同  修改总金额
        Contract contract = contractDao.selectByPrimaryKey(exct.getContractId());
        Double totalAmount = contract.getTotalAmount();
        totalAmount -= amount;
        contract.setTotalAmount(totalAmount);
        //修改附件数
        contract.setExtNum(contract.getExtNum() -1);

        contractDao.updateByPrimaryKeySelective(contract);

        //删除
        extCproductDao.deleteByPrimaryKey(id);

    }
}
